package com.zengdada.authority.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * 数据接口返回类
 *
 * @author 12614
 */
@ApiModel("数据接口返回类")
public class JsonMessage {

    public static ObjectMapper objectMapper = new ObjectMapper();


    public JsonMessage() {

    }

    public JsonMessage(boolean success, String message, int code, Object data, long total) {
        this.success = success;
        this.code = code;
        this.data = data;
        this.total = total;
        this.message = message;
        if (this.code == Integer.MIN_VALUE) {
            this.code = this.success ? 0 : 1;
        }
    }

    /**
     * 标志当前处理是否成功，成功true，失败false
     */
    @ApiModelProperty("标志当前处理是否成功，成功true，失败false")
    private boolean success;
    /**
     * 错误代码:成功0，错误>0
     */
    @ApiModelProperty("错误代码:成功0，错误>0")
    private int code;
    /**
     * 请求返回的数据
     */
    @ApiModelProperty("请求返回的数据")
    private Object data;
    /**
     * 请求数据total
     */
    @ApiModelProperty("数据的总条数")
    private long total;

    /**
     * 返回信息
     */
    @ApiModelProperty("信息")
    private String message;

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String toJsonString() throws JsonProcessingException {
        return objectMapper.writeValueAsString(this);
    }

    public static JsonMessage success() {
        return success(null);
    }

    public static JsonMessage success(Object data) {
        return new JsonMessage(true, "success", 0, data, 0);
    }

    public static JsonMessage success(String message, Object data) {
        return new JsonMessage(true, message, 0, data, 0);
    }

    public static JsonMessage success(long total) {
        return new JsonMessage(true, "success", 0, null, total);
    }

    public static JsonMessage success(Object data, long total) {
        return new JsonMessage(true, "success", 0, data, total);
    }


    public static JsonMessage fail() {
        return fail(null);
    }

    public static JsonMessage fail(String message) {
        return fail(message, 9999);
    }

    public static JsonMessage fail(String message, int code) {
        return fail(message, code, null);
    }

    public static JsonMessage fail(String message, int code, Object data) {
        return new JsonMessage(false, message, code, data, 0);
    }
}
